package cwinsor.raking.year2014;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * The class contains information relating to a rakers .CSV file
 */
public class CsvParseRakers {

	// map from column to property
	private Map<PROP_E,PropertyBase> parsedProps = new HashMap<PROP_E,PropertyBase>();

	/*
	 * constructor
	 */
	public CsvParseRakers() {
	}

	/*
	 * parse String into fields
	 */
	public Boolean parse(String in) {

		// use comma as separator
		String[]  fieldArray = in.split(",");	

		if (fieldArray[0].equals("SKIP") ||
				fieldArray[0].equals("Skip") |
				fieldArray[0].equals("skip") |
				fieldArray[0].equals("header")) {
			if (dL > 0) {
				Msg.note(dS + ": skipping line ");
			}
			return false;
		}

		if (dL > 0) {
			Msg.note(dS + ": adding line " + in + " fieldArray.length " + fieldArray.length);
		}

		// sanity check - every line should have enough fields, and terminate with "END"
		if (fieldArray.length <= 32) {
			Msg.fatal(dS + "the input line does not contain the correct number of fields - expect 33 observe " + fieldArray.length);
		}
		if (!fieldArray[32].equals("END")) {
			Msg.fatal(dS + "the input line does not terminate with END - observed " + fieldArray[32]);
		}


		add(PROP_E.FIRST_NAME, fieldArray[1]);
		add(PROP_E.LAST_NAME, fieldArray[2]);
		add(PROP_E.POWER, fieldArray[3]);
		add(PROP_E.COACH, fieldArray[4]);
		add(PROP_E.GENDER, fieldArray[5]);
		add(PROP_E.VARSITY_NOVICE, fieldArray[6]);
		add(PROP_E.GRADE, fieldArray[7]);

		// raker preference
		add(PROP_E.RP02AM, fieldArray[ 8]);
		add(PROP_E.RP02PM, fieldArray[ 9]);
		add(PROP_E.RP03AM, fieldArray[10]);
		add(PROP_E.RP03PM, fieldArray[11]);
		add(PROP_E.RP09AM, fieldArray[12]);
		add(PROP_E.RP09PM, fieldArray[13]);
		add(PROP_E.RP10AM, fieldArray[14]);
		add(PROP_E.RP10PM, fieldArray[15]);

		// raker last preference
		add(PROP_E.RLP02AM, fieldArray[16]);
		add(PROP_E.RLP02PM, fieldArray[17]);
		add(PROP_E.RLP03AM, fieldArray[18]);
		add(PROP_E.RLP03PM, fieldArray[19]);
		add(PROP_E.RLP09AM, fieldArray[20]);
		add(PROP_E.RLP09PM, fieldArray[21]);
		add(PROP_E.RLP10AM, fieldArray[22]);
		add(PROP_E.RLP10PM, fieldArray[23]);

		// team assignment
		add(PROP_E.TA02AM, fieldArray[24]);
		add(PROP_E.TA02PM, fieldArray[25]);
		add(PROP_E.TA03AM, fieldArray[26]);
		add(PROP_E.TA03PM, fieldArray[27]);
		add(PROP_E.TA09AM, fieldArray[28]);
		add(PROP_E.TA09PM, fieldArray[29]);
		add(PROP_E.TA10AM, fieldArray[30]);
		add(PROP_E.TA10PM, fieldArray[31]);

		return true;
	}

	/*
	 * construct a single PrsnProp of the correct derived class using the prototype in PROPERTY		
	 */
	private void add(PROP_E propE, String string) {
		PropertyBase propO = propE.prototype();
		propO.setPropertyValFromString(string);
		parsedProps.put(propE, propO);
	}

	/*
	 * get the property object, from property enum
	 */
	public PropertyBase getProp(PROP_E propE) {
		return parsedProps.get(propE);
	}

	// generate a comma-separated string given a person
	// first find the greatest column number
	// then initialize the string to just a series of commas
	// then step through the properties to put the property value in the string		
	public String generate(Person person) {

		String string = "raker"
				+ "," + person.getProp(PROP_E.FIRST_NAME).toString()
				+ "," + person.getProp(PROP_E.LAST_NAME).toString()
				+ "," + person.getProp(PROP_E.POWER).toString()
				+ "," + person.getProp(PROP_E.COACH).toString()
				+ "," + person.getProp(PROP_E.GENDER).toString()
				+ "," + person.getProp(PROP_E.VARSITY_NOVICE).toString()
				+ "," + person.getProp(PROP_E.GRADE).toString()

				// raker preference
				+ "," + person.getProp(PROP_E.RP02AM).toString()
				+ "," + person.getProp(PROP_E.RP02PM).toString()
				+ "," + person.getProp(PROP_E.RP03AM).toString()
				+ "," + person.getProp(PROP_E.RP03PM).toString()
				+ "," + person.getProp(PROP_E.RP09AM).toString()
				+ "," + person.getProp(PROP_E.RP09PM).toString()
				+ "," + person.getProp(PROP_E.RP10AM).toString()
				+ "," + person.getProp(PROP_E.RP10PM).toString()

				// raker last preference
				+ "," + person.getProp(PROP_E.RLP02AM).toString()
				+ "," + person.getProp(PROP_E.RLP02PM).toString()
				+ "," + person.getProp(PROP_E.RLP03AM).toString()
				+ "," + person.getProp(PROP_E.RLP03PM).toString()
				+ "," + person.getProp(PROP_E.RLP09AM).toString()
				+ "," + person.getProp(PROP_E.RLP09PM).toString()
				+ "," + person.getProp(PROP_E.RLP10AM).toString()
				+ "," + person.getProp(PROP_E.RLP10PM).toString()

				// team assignment
				+ "," + person.getProp(PROP_E.TA02AM).toString()
				+ "," + person.getProp(PROP_E.TA02PM).toString()
				+ "," + person.getProp(PROP_E.TA03AM).toString()
				+ "," + person.getProp(PROP_E.TA03PM).toString()
				+ "," + person.getProp(PROP_E.TA09AM).toString()
				+ "," + person.getProp(PROP_E.TA09PM).toString()
				+ "," + person.getProp(PROP_E.TA10AM).toString()
				+ "," + person.getProp(PROP_E.TA10PM).toString()
				+ "," + "END";
		return string;
	}

	// debug string, debug level
	String  dS = this.getClass().getSimpleName();
	Integer dL = 0;
}

